var name = '';
var mobile = '';
var recommend_name = '';
var recommend_mobile = '';
$(function(){
    var code = GetQueryString('code')?GetQueryString('code'):0;
    if(code == 1001){
        alertTips("请输入您的姓名和<br>正确的手机号");
    }
    if(code == 1003){
        alertTips("请输入您好友的姓名和<br>好友的正确手机号");
    }
    if(code == 1002){
        alertTips("很抱歉，您好友已经领取<br>过红包，或您好友不是新用户");
    }
    if(code == 1004){
        alert("亲爱的，该活动不支持自荐哦");
    }
    if(code == 1000){
        $('.success,.black_bg_all').fadeIn(500);
    }
    $("#name").bind('input',function(){
        name = $("#name").val();
        if(name.length > 0){
            if(mobile.length == 11 && recommend_name.length > 0 && recommend_mobile.length == 11){
                $(".undraw").attr("src", "/Public/image/draw.png");
                $("a").attr("onclick", "submitAll()");
            }else{
                $("a").removeAttr("onclick");
                $(".undraw").attr("src", "/Public/image/undraw.png");
            }
        }else{
            $("a").removeAttr("onclick");
            $(".undraw").attr("src", "/Public/image/undraw.png");
        }
    });

    $("#recommend_name").bind('input',function(){
        recommend_name = $("#recommend_name").val();
        if(recommend_name.length > 0){
            if(mobile.length == 11 && name.length > 0 && recommend_mobile.length == 11){
                $(".undraw").attr("src", "/Public/image/draw.png");
                $("a").attr("onclick", "submitAll()");
            }else{
                $("a").removeAttr("onclick");
                $(".undraw").attr("src", "/Public/image/undraw.png");
            }
        }else{
            $("a").removeAttr("onclick");
            $(".undraw").attr("src", "/Public/image/undraw.png");
        }
    });

    $("#mobile").bind('input',function(){
        mobile = $("#mobile").val();
        if(mobile.length == 11){
            if(name.length > 0 && recommend_name.length > 0 && recommend_mobile.length == 11){
                $(".undraw").attr("src", "/Public/image/draw.png");
                $("a").attr("onclick", "submitAll()");
            }else{
                $(".undraw").attr("src", "/Public/image/undraw.png");
                $("a").removeAttr("onclick");
            }
        }else{
            $(".undraw").attr("src", "/Public/image/undraw.png");
            $("a").removeAttr("onclick");
        }
    });

    $("#recommend_mobile").bind('input',function(){
        recommend_mobile = $("#recommend_mobile").val();
        if(recommend_mobile.length == 11){
            if(name.length > 0 && recommend_name.length > 0 && mobile.length == 11){
                $(".undraw").attr("src", "/Public/image/draw.png");
                $("a").attr("onclick", "submitAll()");
            }else{
                $(".undraw").attr("src", "/Public/image/undraw.png");
                $("a").removeAttr("onclick");
            }
        }else{
            $(".undraw").attr("src", "/Public/image/undraw.png");
            $("a").removeAttr("onclick");
        }
    });

    $(".cha").click(function(){
        window.location.href = '/Home/Webapp/activity';
    });
});

function submitAll()
{
    if(name.length <= 0){
        alert("亲爱的，请输入您的姓名");
        return false;
    }
    if(mobile.length < 11 || !(/^1[3|4|5|7|8]\d{9}$/.test(mobile))){
        alert("亲爱的，请输入正确的手机号");
        return false;
    }
    if(recommend_name.length <= 0){
        alert("亲爱的，请输入您好友的姓名");
        return false;
    }
    if(recommend_mobile.length < 11 || !(/^1[3|4|5|7|8]\d{9}$/.test(mobile))){
        alert("亲爱的，请输入您好友的正确手机号");
        return false;
    }
    if(recommend_mobile == mobile){
        alert("亲爱的，该活动不支持自荐哦", false);
        return false;
    }
    document.getElementById('subform').submit();
    return true;
}

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null) return  unescape(r[2]); return null;
}
